import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
let store = new Vuex.Store({
    // 1. state
    state:{
        orderStatus:10,
        shoppingList:[],
        orderList: [],
        chooseAddress: {}, // 选择的当前地址信息
        address:null,//默认地址
        addressId:null,//默认地址id
    },

    // // 2. getters
    getters:{
        // 参数列表state指的是state数据
        getsState(state){
            return state.orderStatus;
        },
        getsShopping(state){
            return state.shoppingList;
        },
        getsOrderList(state){
            return state.orderList;
        },
        getsAddress(state){
            return state.address; 
        },
        getsAddressId(state){
            return state.addressId; 
        }
    },
    // 3. actions
    // 通常跟api接口打交道
    actions:{
        // 设置城市信息
        // 参数列表：{commit, state}
        // state指的是state数据
        // commit调用mutations的方法 
        // name就是调用此方法时要传的参数
        setCityName({commit,state}, name){
            commit("setCity", name);
        },
        setShopping({commit,state}, name){
            commit("setShopList", name);
        },
        setOrder({commit,state}, name){
            commit("setOrderList", name);
        },
        chooseAddressData({commit,state}, name){
            commit("chooseAddress", name);
        },
        setAddressData({commit,state}, name){
            commit("setAddress", name);
        },
        setAddressDataid({commit,state}, name){
            commit("setAddressid", name);
        }
    },
    // 4. mutations
    mutations:{
        // state指的是state的数据
        // name传递过来的数据
        setCity(state, name){
            state.orderStatus = name;//将传参设置给state的orderStatus
        },
        setShopList(state, name){
            state.shoppingList = name;
        },
        setOrderList(state, name){
            const arr = []
            arr.push(name)
            state.orderList = arr
        },
        setAddress(state, name){
            state.address = name;
        },
        chooseAddress(state, name){
            state.chooseAddress = name;
        },
        setAddressid(state, name){
            state.addressId = name;
        }
    }
});

export default store;